package domain;



import java.math.BigDecimal;
import java.util.List;

public class AccountService {
	private Account account;
	
	
	private static List<Account> accountList;
	
	public AccountService() {
		//初始化账号数据
		account =new Account();
		
		accountList = account.init();
	}

	public Account getAccountByCard(Card card) {
		//根据card来查询账户，如果有则返回账户没有则为空
		for(Account a : accountList) {
			if(a.getCardId().equals(card.getCardId())) {
				return a;
			}
		}
		return null;
	}

	public Account validate(Card card, String password) {
		//匹配账号密码
		for(Account a : accountList) {
			if(a.getCardId().equals(card.getCardId())) {
				if(a.getPassword().equals(password))
				return a;
			}
		}
		return null;

	}
	
	public static boolean withdrawl(Account account,double amount) {
	    if(amount!=0){
		BigDecimal a = new BigDecimal(amount);
		//如果取款金额大于0
		//如果账户余额大于取款金额
		if(account.getBalance().compareTo(a)>=0) {
			account.setBalance(account.getBalance().subtract(a));
			return true;
		}
		}
		return false;
	}

}
